package com.education.platform.api.controller.paper;

import com.education.platform.api.config.ThreadUser;
import com.education.platform.api.constants.RedisCacheConstant;
import com.education.platform.api.service.QuestionBankService;
import com.education.platform.api.service.UserPointsService;
import com.education.platform.api.service.common.CacheDataService;
import com.education.platform.api.vo.request.ChallengeResultVO;
import com.education.platform.api.vo.request.CommitChallengeVO;
import com.education.platform.api.vo.response.ChallengeQuestionResponseVO;

import com.education.platform.common.enums.QuestionTypeEnum;
import com.education.platform.common.redis.RedisUtils;
import com.education.platform.dao.entity.QuestionBankDO;

import com.education.platform.common.util.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.validation.annotation.Validated;


/**
 * describe: 题库
 *
 * @author : tomato
 * @date: 2020-06-22 11:44:30.654
 **/
@RestController
@Api("题库")
@RequestMapping("questionBank")
@Validated
public class QuestionBankController {

    private static final Logger LOGGER = LoggerFactory.getLogger(QuestionBankController.class);

    @Autowired
    private QuestionBankService questionBankService;

    @Autowired
    private UserPointsService userPointsService;

    @Autowired
    CacheDataService cacheDataService;

    @Autowired
    private RedisUtils redisUtils;


    /**
     * 挑战答题题目
     *
     * @param
     * @return
     */
    @ApiOperation(value = "挑战答题题目")
    @PostMapping("getChallengeQuestion")
    public List<ChallengeQuestionResponseVO> getChallengeQuestion() {
        QuestionBankDO query = new QuestionBankDO();
        List<QuestionBankDO> list = questionBankService.getRandomList(query);
        List<ChallengeQuestionResponseVO> voList = new ArrayList<>();
        list.forEach(m -> {
            ChallengeQuestionResponseVO vo = BeanUtil.convert(m, ChallengeQuestionResponseVO.class);
            vo.setOptions(Arrays.asList(m.getOptionA().split("==")));
            if (m.getType() == QuestionTypeEnum.SELECT.getValue()
                    || m.getType() == QuestionTypeEnum.SIMPLE.getValue()) {
                vo.setChoiceAnswer(Arrays.asList(m.getChoiceAnswer()));
            } else {
                vo.setChoiceAnswer(Arrays.asList(m.getChoiceAnswer().split("==")));
            }
            voList.add(vo);
        });

        return voList;
    }


    /**
     * 提交挑战结果
     *
     * @param
     * @return
     */
    @ApiOperation(value = "提交挑战结果")
    @PostMapping("commitChallengeResult")
    public ChallengeResultVO commitChallengeResult(@RequestBody @Valid CommitChallengeVO vo) {
        ChallengeResultVO resultVO = userPointsService.challenge(vo.getPoints(), vo.getNumbers(), vo.getBusinessType());
        return resultVO;
    }

    /**
     * 提交挑战结果
     *
     * @param
     * @return
     */
    @ApiOperation(value = "获取剩余挑战次数")
    @GetMapping("getChallengeTime")
    public Integer getChallengeTime() {
       return cacheDataService.getChallengeTime();
    }

    /**
     * 挑战次数减一
     *
     * @param
     * @return
     */
    @ApiOperation(value = "剩余挑战次数减一")
    @PostMapping("decrChallengeTime")
    public Boolean decrChallengeTime() {
        redisUtils.hincrBy( RedisCacheConstant.HASH_KEY_DAILY_CHALLENGE_NUM,String.valueOf(ThreadUser.getUserId()),1);
        return Boolean.TRUE;
    }

}
